home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / demos / texsub.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-27  |  6.2 KB  |  221 lines

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /*  texsub.c
  39.  *  This program texture maps a checkerboard image onto
  40.  *  two rectangles.  This program clamps the texture, if
  41.  *  the texture coordinates fall outside 0.0 and 1.0.
  42.  *  If the s key is pressed, a texture subimage is used to
  43.  *  alter the original texture.  If the r key is pressed, 
  44.  *  the original texture is restored.
  45.  */
  46.  
  47. /*
  48.  * MiniGL adaption by Hans-Joerg Frieden
  49.  * December 1999
  50.  */
  51.  
  52. #include <mgl/gl.h>
  53.  
  54. #include <stdlib.h>
  55. #include <stdio.h>
  56. #include <math.h>
  57.  
  58. /*  Create checkerboard textures  */
  59. #define checkImageWidth 64
  60. #define checkImageHeight 64
  61. #define subImageWidth 16
  62. #define subImageHeight 16
  63. static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
  64. static GLubyte subImage[subImageHeight][subImageWidth][4];
  65.  
  66. static GLuint texName;
  67.  
  68. void makeCheckImages(void)
  69. {
  70.    int i, j, c;
  71.     
  72.    for (i = 0; i < checkImageHeight; i++) {
  73.       for (j = 0; j < checkImageWidth; j++) {
  74.      c = ((((i&0x8)==0)^((j&0x8)==0)))*255;
  75.      checkImage[i][j][0] = (GLubyte) c;
  76.      checkImage[i][j][1] = (GLubyte) c;
  77.      checkImage[i][j][2] = (GLubyte) c;
  78.      checkImage[i][j][3] = (GLubyte) 255;
  79.       }
  80.    }
  81.    for (i = 0; i < subImageHeight; i++) {
  82.       for (j = 0; j < subImageWidth; j++) {
  83.      c = ((((i&0x4)==0)^((j&0x4)==0)))*255;
  84.      subImage[i][j][0] = (GLubyte) c;
  85.      subImage[i][j][1] = (GLubyte) 0;
  86.      subImage[i][j][2] = (GLubyte) 0;
  87.      subImage[i][j][3] = (GLubyte) 255;
  88.       }
  89.    }
  90. }
  91.  
  92. void init(void)
  93. {    
  94.    glClearColor (0.0, 0.0, 0.0, 0.0);
  95.    glShadeModel(GL_FLAT);
  96.    glEnable(GL_DEPTH_TEST);
  97.  
  98.    makeCheckImages();
  99.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  100.  
  101.    glGenTextures(1, &texName);
  102.    glBindTexture(GL_TEXTURE_2D, texName);
  103.  
  104.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  105.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  106.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  107.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  108.    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 
  109.         0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
  110. }
  111.  
  112. void display(void)
  113. {
  114.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  115.    glEnable(GL_TEXTURE_2D);
  116.    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  117.    glBindTexture(GL_TEXTURE_2D, texName);
  118.    glBegin(GL_QUADS);
  119.    glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
  120.    glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
  121.    glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
  122.    glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
  123.  
  124.    glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
  125.    glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
  126.    glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
  127.    glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
  128.    glEnd();
  129.    glFlush();
  130.    glDisable(GL_TEXTURE_2D);
  131.    mglUnlockDisplay();
  132.    mglSwitchDisplay();
  133. }
  134.  
  135. void reshape(int w, int h)
  136. {
  137.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  138.    glMatrixMode(GL_PROJECTION);
  139.    glLoadIdentity();
  140.    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
  141.    glMatrixMode(GL_MODELVIEW);
  142.    glLoadIdentity();
  143.    glTranslatef(0.0, 0.0, -3.6);
  144. }
  145.  
  146. /* ARGSUSED1 */
  147. static void keyboard (char key)
  148. {
  149.    switch (key) {
  150.       case 's':
  151.       case 'S':
  152.      glBindTexture(GL_TEXTURE_2D, texName);
  153.      glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 44, subImageWidth,
  154.              subImageHeight, GL_RGBA,
  155.              GL_UNSIGNED_BYTE, subImage);
  156.      break;
  157.       case 'r':
  158.       case 'R':
  159.      glBindTexture(GL_TEXTURE_2D, texName);
  160.      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
  161.               checkImageHeight, 0, GL_RGBA,
  162.               GL_UNSIGNED_BYTE, checkImage);
  163.      break;
  164.       case 27:
  165.      mglExit();
  166.      break;
  167.       default:
  168.      break;
  169.    }
  170. }
  171.  
  172. static void
  173. idle(void)
  174. {
  175.     display();
  176. }
  177.  
  178.  
  179. int main(int argc, char *argv[])
  180. {
  181.     GLint width=320; GLint height=240;
  182.     int i;
  183.  
  184.     for (i=1; i<argc; i++)
  185.     {
  186.     if (0 == stricmp(argv[i], "-width"))
  187.     {
  188.         i++;
  189.         width = atoi(argv[i]);
  190.     }
  191.     if (0 == stricmp(argv[i], "-height"))
  192.     {
  193.         i++;
  194.         height = atoi(argv[i]);
  195.     }
  196.     if (0 == stricmp(argv[i], "-window"))
  197.     {
  198.         mglChooseWindowMode(GL_TRUE);
  199.     }
  200.  
  201.     }
  202.  
  203.     mglChooseVertexBufferSize(1000);
  204.     MGLInit();
  205.     mglCreateContext(0,0,width,height);
  206.     mglEnableSync(GL_TRUE);
  207.  
  208.     init();
  209.  
  210.     glClearColor(0.0, 0.0, 0.0, 1.0);
  211.  
  212.     reshape(width,height);
  213.     mglLockMode(MGL_LOCK_AUTOMATIC);
  214.     mglIdleFunc(idle);
  215.     mglKeyFunc(keyboard);
  216.     mglMainLoop();
  217.     mglDeleteContext();
  218.     MGLTerm();
  219.     return 0;             /* ANSI C requires main to return int. */
  220. }
  221.